/*
 * aht20.c
 *
 *  Created on: Oct 25, 2025
 *      Author: AND
 */
#include "aht20.h"
#define AHT20_ADDRESS 0x70
uint8_t rcvBuffer[6] = {0};
//轮询模式写法
//void AHT20_Init()
//{
//	uint8_t state;
//	HAL_Delay(40);
//	HAL_I2C_Master_Receive(&hi2c1,AHT20_ADDRESS, &state, 1, HAL_MAX_DELAY);
//	if((state & 0x08) == 0x00)
//	{
//		uint8_t sendBuffer[3] = {0xBE,0x08,0x00};
//		HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer, 3, 1000);
//	}
//}
//void AHT20_Read(float* ptemperature,float* phumidity)
//{
//	uint8_t sendBuffer[3] = {0xAC,0x33,0x00};
//	uint8_t rcvBuffer[6];
//	HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer, 3, 1000);
//	HAL_Delay(75);
//	HAL_I2C_Master_Receive(&hi2c1, AHT20_ADDRESS, rcvBuffer, 6, 1000);
//	if((rcvBuffer[0]&0x80) == 0x00)
//	{
//		uint32_t data = 0;
//		data = ((uint32_t)rcvBuffer[1]<<12) + ((uint32_t)rcvBuffer[3]>>4)+((uint32_t)rcvBuffer[2]<<4);
//		*phumidity = data*100.0f / (1 << 20);
//		data = ((uint32_t)rcvBuffer[5])+((uint32_t)rcvBuffer[4]<<8)+(((uint32_t)rcvBuffer[3]&0x0F)<<16);
//		*ptemperature = data *200.0f / (1<<20) - 50;
//	}
//}
// 中断模式写法：
/*
 * 状态机：
 * 不同状态下做不同的事。
 * 此处直接把HAL_I2C_Master_Receive_IT(&hi2c1,AHT20_ADDRESS, &state, 1);放在Read里——
 * 只是把任务交给外设后就继续向下执行，并不会等待数据发送/接收完成。
 * -- 1. 为什么发送测量指令的时候 需要Static
 * HAL_I2C_Master_Transmit_IT 是 非阻塞 的，它会立即返回，实际发送是在中断中完成的。
 * 如果你把 sendBuffer 定义为 局部变量（非 static），它会在函数返回后被销毁，中断服务程序再去访问它就会导致内存错误（野指针）。
 * -- 2. 为什么不在while循环里 state=0时 Init()
 * 初始化只需要做一次，不能重复
	AHT20_Init() 的作用是 上电初始化传感器（校准、配置），只需要在上电时执行一次。
	如果你把它放在 while(1) 的主循环里，state == 0 时会 反复初始化，可能导致：
	传感器异常
	I2C 总线冲突
	状态机逻辑混乱
	-- 3 状态机思维：
	你可以把状态机想象成一个流水线工人（掌管着机器、中断服务程序），他每一步只干一件事，干完就贴个标签，
	告诉下一个工人“该你干了”（改 state）。
	Init并非采用非阻塞，而是阻塞方式
 */
void AHT20_Init()
{
	uint8_t state;
	HAL_Delay(40);
	HAL_I2C_Master_Receive(&hi2c1,AHT20_ADDRESS, &state, 1, HAL_MAX_DELAY);
	if((state & 0x08) == 0x00)
	{
		uint8_t sendBuffer[3] = {0xBE,0x08,0x00};
		HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer, 3, 1000);
	}
}

void AHT20_Read(float* ptemperature,float* phumidity)
{
	uint8_t sendBuffer[3] = {0xAC,0x33,0x00};
	uint8_t rcvBuffer[6];
	HAL_I2C_Master_Transmit_IT(&hi2c1, AHT20_ADDRESS, sendBuffer, 3);
	HAL_Delay(75);
	HAL_I2C_Master_Receive_IT(&hi2c1, AHT20_ADDRESS, rcvBuffer, 6);
	if((rcvBuffer[0]&0x80) == 0x00)
	{
		uint32_t data = 0;
		data = ((uint32_t)rcvBuffer[1]<<12) + ((uint32_t)rcvBuffer[3]>>4)+((uint32_t)rcvBuffer[2]<<4);
		*phumidity = data*100.0f / (1 << 20);
		data = ((uint32_t)rcvBuffer[5])+((uint32_t)rcvBuffer[4]<<8)+(((uint32_t)rcvBuffer[3]&0x0F)<<16);
		*ptemperature = data *200.0f / (1<<20) - 50;
	}
}
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
  if(hi2c == &hi2c1)
  {
	  state = 2;
  }
}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	if(hi2c == &hi2c1)
	  {
		  state = 4;
	  }
}
void AHT20_Measure()
{
	static uint8_t sendBuffer[3] = {0xAC,0x33,0x00};
	HAL_I2C_Master_Transmit_DMA(&hi2c1, AHT20_ADDRESS, sendBuffer, 3);// IT
}
void AHT20_Get()
{
	//HAL_Delay(75);
	HAL_I2C_Master_Receive_DMA(&hi2c1, AHT20_ADDRESS, rcvBuffer, 6);// IT
}
void AHT20_Analysis(float* ptemperature,float* phumidity)
{
	if((rcvBuffer[0]&0x80) == 0x00)
		{
			uint32_t data = 0;
			data = ((uint32_t)rcvBuffer[1]<<12) + ((uint32_t)rcvBuffer[3]>>4)+((uint32_t)rcvBuffer[2]<<4);
			*phumidity = data*100.0f / (1 << 20);
			data = ((uint32_t)rcvBuffer[5])+((uint32_t)rcvBuffer[4]<<8)+(((uint32_t)rcvBuffer[3]&0x0F)<<16);
			*ptemperature = data *200.0f / (1<<20) - 50;
		}
}

